﻿<Fluent:RibbonWindow x:Class="VisualJsonEditor.Views.MainWindow"
                     xmlns:Fluent="clr-namespace:Fluent;assembly=Fluent"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:viewModels="clr-namespace:VisualJsonEditor.ViewModels"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                     xmlns:converters="clr-namespace:MyToolkit.Converters;assembly=MyToolkit.Extended"
                     xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
                     xmlns:localization="clr-namespace:VisualJsonEditor.Localization"
                     xmlns:controls="clr-namespace:VisualJsonEditor.Controls"
                     xmlns:models="clr-namespace:VisualJsonEditor.Models"
                     mc:Ignorable="d" Title="{x:Static localization:Strings.HeaderMainWindow}" 
                     Height="600" Width="700" MinWidth="400" MinHeight="400" Background="LightGray"
                     WindowStartupLocation="CenterScreen">

    <Window.Resources>
        <viewModels:MainWindowModel x:Key="ViewModel" />
        <converters:VisibilityConverter x:Key="VisibilityConverter" />
        <converters:NotConverter x:Key="NotConverter" />
    </Window.Resources>

    <Grid DataContext="{StaticResource ViewModel}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <!-- ribbons -->
        <Fluent:Ribbon ShowQuickAccessToolBarAboveRibbon="True" 
                       IsQuickAccessToolBarVisible="True" x:Name="Ribbon"
                       AutomaticStateManagement="True" Grid.ColumnSpan="3">
            
            <!--Backstage-->
            <Fluent:Ribbon.Menu>
                <Fluent:Backstage>
                    <Fluent:BackstageTabControl>
                        <Fluent:BackstageTabItem Header="{Binding Strings.HeaderRecentDocuments}">
                            <Grid Margin="8,0,0,0">
                                <ScrollViewer VerticalScrollBarVisibility="Visible">
                                    <ItemsControl ItemsSource="{Binding Configuration.RecentFiles}" Margin="0,0,8,0">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <Button Click="OnOpenDocument" HorizontalContentAlignment="Left" 
                                                        Background="Transparent" BorderThickness="0" Margin="0,8,0,0"
                                                        Command="{Binding OpenDocumentFromPathCommand, Source={StaticResource ViewModel}}"
                                                        CommandParameter="{Binding FilePath}">
                                                    <Button.Content>
                                                        <StackPanel Orientation="Horizontal" Margin="4">
                                                            <Image Source="/Images/ButtonCreateLarge.png" Margin="0,0,4,0" />
                                                            <StackPanel>
                                                                <TextBlock Text="{Binding FileName}" FontSize="16" />
                                                                <TextBlock Text="{Binding FilePath}" FontSize="10" />
                                                            </StackPanel>
                                                        </StackPanel>
                                                    </Button.Content>
                                                </Button>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </ScrollViewer>
                            </Grid>
                        </Fluent:BackstageTabItem>

                        <Fluent:Button Header="{Binding Strings.ButtonNew}" Width="200"
                                       Command="{Binding CreateDocumentCommand}" 
                                       ToolTip="{Binding Strings.ShortcutCtrlN}"
                                       Icon="/Images/ButtonCreateLarge.png" />

                        <Fluent:Button Header="{Binding Strings.ButtonOpen}" 
                                       Command="{Binding OpenDocumentCommand}" 
                                       ToolTip="{Binding Strings.ShortcutCtrlO}"
                                       Icon="/Images/ButtonOpenLarge.png" />

                        <Fluent:Button Header="{Binding Strings.ButtonSave}"  
                                       Command="{Binding SaveDocumentCommand}" CommandParameter="{Binding SelectedDocument}"
                                       ToolTip="{Binding Strings.ShortcutCtrlS}"
                                       Icon="/Images/ButtonSaveLarge.png" />
                        
                        <Fluent:Button Header="{Binding Strings.ButtonSaveAs}" 
                                       Command="{Binding SaveDocumentAsCommand}" CommandParameter="{Binding SelectedDocument}"
                                       Icon="/Images/ButtonSaveLarge.png" />
                        <Fluent:Button Header="{Binding Strings.ButtonSaveSchemaAs}" 
                                       Command="{Binding SaveDocumentSchemaAsCommand}" CommandParameter="{Binding SelectedDocument}"
                                       Icon="/Images/ButtonSaveLarge.png" />
                        
                        <Fluent:Button Header="{Binding Strings.ButtonExit}" 
                                       Click="OnExitApplication" Icon="/Images/ButtonExitSmall.png" KeyTip="X" />
                    </Fluent:BackstageTabControl>
                </Fluent:Backstage>
            </Fluent:Ribbon.Menu>
            
            <Fluent:Ribbon.QuickAccessItems>
                <Fluent:QuickAccessMenuItem Icon="/Images/ButtonSaveLarge.png" 
                                            Command="{Binding SaveDocumentCommand}" CommandParameter="{Binding SelectedDocument}" />
                <Fluent:QuickAccessMenuItem Icon="/Images/ButtonUndoSmall.png" 
                                            Command="{Binding UndoCommand}" CommandParameter="{Binding SelectedDocument}" />
                <Fluent:QuickAccessMenuItem Icon="/Images/ButtonRedoSmall.png" 
                                            Command="{Binding RedoCommand}" CommandParameter="{Binding SelectedDocument}" />
            </Fluent:Ribbon.QuickAccessItems>
            
            <!--Tabs-->
            <Fluent:RibbonTabItem Header="Home" KeyTip="H" >
                <Fluent:RibbonGroupBox Header="{Binding Strings.RibbonGroupFile}">
                    <Fluent:Button Icon="/Images/ButtonCloseSmall.png" Padding="4,0,4,0"
                                   LargeIcon="/Images/ButtonCloseLarge.png"
                                   Header="{Binding Strings.ButtonClose}" 
                                   ToolTip="{Binding Strings.ShortcutCtrlW}"
                                   Command="{Binding CloseDocumentCommand}" CommandParameter="{Binding SelectedDocument}" />
                    <Fluent:Button Icon="/Images/ButtonValidateSmall.png" Padding="4,0,4,0"
                                   LargeIcon="/Images/ButtonValidateLarge.png"
                                   Header="{Binding Strings.ButtonValidate}" 
                                   ToolTip="{Binding Strings.ShortcutCtrlU}"
                                   Command="{Binding ValidateDocumentCommand}" CommandParameter="{Binding SelectedDocument}" />
                </Fluent:RibbonGroupBox>
                <Fluent:RibbonGroupBox Header="{Binding Strings.RibbonGroupMore}">
                    <Fluent:Button Icon="/Images/ButtonAboutSmall.png" Padding="4,0,4,0"
                                   LargeIcon="/Images/ButtonAboutLarge.png"
                                   Header="{Binding Strings.ButtonAbout}" Click="OnShowAboutWindow" />
                </Fluent:RibbonGroupBox>
            </Fluent:RibbonTabItem>
        </Fluent:Ribbon>
        
        <!-- documents -->
        <xcad:DockingManager DocumentsSource="{Binding Documents}" Grid.Row="1" Margin="0,8,0,0"
                             ActiveContent="{Binding SelectedDocument, Mode=TwoWay}"
                             x:Name="DockingManager" DocumentClosing="OnDocumentClosing">
            <!-- document template -->
            <xcad:DockingManager.LayoutItemTemplate>
                <DataTemplate>
                    <ScrollViewer Margin="-1" d:DataContext="{d:DesignInstance Type=models:JsonDocumentModel}" Focusable="False">
                        <Border IsEnabled="{Binding IsLoading, Source={StaticResource ViewModel}, Converter={StaticResource NotConverter}}">
                            <controls:JsonEditor Margin="8" Data="{Binding Data}" 
                                                 IsEnabled="{Binding IsReadOnly, Converter={StaticResource NotConverter}}" />
                        </Border>
                    </ScrollViewer>
                </DataTemplate>
            </xcad:DockingManager.LayoutItemTemplate>
            <!-- header on floating windows -->
            <xcad:DockingManager.DocumentTitleTemplate>
                <DataTemplate>
                    <Border d:DataContext="{d:DesignInstance Type=xcad:LayoutDocument}" Margin="6,0,0,0">
                        <StackPanel Orientation="Horizontal" DataContext="{Binding Content}" 
                                    d:DataContext="{d:DesignInstance Type=models:JsonDocumentModel}">
                            <TextBlock Text="{Binding DisplayTitle}" />
                            <TextBlock Text="{Binding Strings.LabelReadOnly, Source={StaticResource ViewModel}}" 
                                       Visibility="{Binding IsReadOnly, Converter={StaticResource VisibilityConverter}}" />
                            <TextBlock Text=" *" Width="8" 
                                       Visibility="{Binding UndoRedoManager.CanUndo, Converter={StaticResource VisibilityConverter}}" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </xcad:DockingManager.DocumentTitleTemplate>
            <!-- header on tabs -->
            <xcad:DockingManager.DocumentHeaderTemplate>
                <DataTemplate>
                    <Border d:DataContext="{d:DesignInstance Type=xcad:LayoutDocument}" Margin="6,2,0,0">
                        <StackPanel Orientation="Horizontal" DataContext="{Binding Content}" 
                                    ToolTip="{Binding FilePath}" d:DataContext="{d:DesignInstance Type=models:JsonDocumentModel}">
                            <TextBlock Text="{Binding DisplayTitle}" />
                            <TextBlock Text="{Binding Strings.LabelReadOnly, Source={StaticResource ViewModel}}" 
                                       Visibility="{Binding IsReadOnly, Converter={StaticResource VisibilityConverter}}" />
                            <TextBlock Text=" *" Width="8" Visibility="{Binding UndoRedoManager.CanUndo, Converter={StaticResource VisibilityConverter}}" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </xcad:DockingManager.DocumentHeaderTemplate>
            <!-- header in pane menu -->
            <xcad:DockingManager.DocumentPaneMenuItemHeaderTemplate>
                <DataTemplate>
                    <Border d:DataContext="{d:DesignInstance Type=xcad:LayoutDocument}">
                        <StackPanel Orientation="Horizontal" DataContext="{Binding Content}" 
                                    d:DataContext="{d:DesignInstance Type=models:JsonDocumentModel}">
                            <TextBlock Text="{Binding DisplayTitle}" />
                            <TextBlock Text=" *" Width="8" Visibility="{Binding UndoRedoManager.CanUndo, Converter={StaticResource VisibilityConverter}}" />
                            <TextBlock Width="8" Visibility="{Binding UndoRedoManager.CanUndo, Converter={StaticResource NotConverter}}" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </xcad:DockingManager.DocumentPaneMenuItemHeaderTemplate>
        </xcad:DockingManager>

        <!-- status bar -->
        <Fluent:StatusBar Grid.Row="2">
            <Fluent:StatusBarItem HorizontalAlignment="Left" Foreground="White" Margin="4,0,0,0"
                                  Content="{Binding SelectedDocument.FilePath}" />
            
            <Fluent:StatusBarItem HorizontalAlignment="Left" Foreground="White" Margin="8,0,0,0"
                                  Content="{Binding SelectedDocument.SchemaPath}" />

            <Fluent:StatusBarItem HorizontalAlignment="Right" Margin="4,0,0,0"
                                  Visibility="{Binding IsLoading, Converter={StaticResource VisibilityConverter}}">
                <ProgressBar IsIndeterminate="{Binding IsLoading}" IsEnabled="{Binding IsLoading}" Width="100" Height="14" />
            </Fluent:StatusBarItem>
        </Fluent:StatusBar>
    </Grid>
</Fluent:RibbonWindow>
